<template>
	<view>
		<view class="" style="width: 100%;height:80%" v-if="tk_img">
			<img :src="tk_img" alt="" style="width: 100%;height:100%" />
		</view>
		<view class="dis disAl disJuB  mar">
			<!-- <view class="mar mar-top20 borRad20 dis disAl disJuC coFFF hei60"
				style="background-color: green;padding:20rpx;font-size:12px" @click.stop="wxshare">
				分享给好友
			</view>
			<view class=" mar mar-top20 borRad20 dis disAl disJuC coFFF hei60"
				style="background-color: green;padding:20rpx;font-size:12px" @click.stop="wxshare">
				分享到朋友圈
			</view> -->
			<view class=" mar mar-top20 borRad20 dis disAl disJuC coFFF hei60"
				style="background-color: green;padding:20rpx;font-size:16px" @click.stop="savephone">
				长按图片保存，发送给朋友
			</view>
		</view>
		<view style="height:50px"></view>

	</view>
</template>

<script>
	import {
		Goods
	} from '../../model/integralgoods-model';
	var Goods2 = new Goods();

	import {
		User
	} from '../../model/user-model.js';
	var user = new User();
	var jweixin = require('jweixin-module');
	export default {
		data() {
			return {
				tk_img: '',
				tg_img: '',
				list: null
			}
		},
		onLoad(option) {
			if (option.pid) {
				console.log('pid',option.pid)
				// 判断是否登录
				if(!uni.getStorageSync('token')){
					
					uni.setStorageSync('pid', option.pid)
					uni.redirectTo({
						url: '/pages/index/index?pid='+option.pid
					})
				}else{
					var params = {}
					user.getuserid((data) => {
						console.log(data)
						if (data.code === 1) {
							if(data.data.uid != option.pid){
								uni.setStorageSync('pid', option.pid)
								uni.redirectTo({
									url: '/pages/index/index?pid='+option.pid
								})
							}
						} else {
							uni.setStorageSync('pid', option.pid)
							uni.redirectTo({
								url: '/pages/index/index?pid='+option.pid
							})
						}
					}, params)
				}
				
				console.log('收到PID')
			}
			this.tg_img = uni.getStorageSync('tg_img')
			this.getList()
			this.getdata()
		},
		methods: {
			getList() {
				var that = this
				let params = {}
				user.getPromotion((data) => {
					console.log(data)
					if (data.code === 0) {
						that.list = data.data
					} else {
						uni.showToast({
							title: data.msg,
							icon: 'none',
							duration: 2000
						})
					}
				}, params)
			},
			wxshare() {
				var that = this
				var surl = encodeURIComponent(window.location.href.split('#')[0]); //据说可以解决URL中带参数的问题，前台用的js编码，后台php解码
				console.log(surl)
				console.log(window.location.href)
				let params = {
					weburl: encodeURIComponent(window.location.href)
				}
				Goods2.jsapiParams((data) => {
					console.log(data)
					if (data.code === 1) {
						jweixin.config({
							debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。  
							appId: data.data.appid, // 必填，公众号的唯一标识  
							timestamp: data.data.timestamp, // 必填，生成签名的时间戳  
							nonceStr: data.data.noncestr, // 必填，生成签名的随机串  
							signature: data.data.signature, // 必填，签名，见附录1  
							surl: data.data.url, //自己添加的，debug为true的时候可以网页打印出对应的URL是否正确  
							jsApiList: ["updateAppMessageShareData", "updateTimelineShareData"]
						});
						jweixin.ready(() => {
							//自定义“分享给朋友”及“分享到QQ”按钮的分享内容（1.4.0）
							jweixin.updateAppMessageShareData({
								title: '由' + that.list.nickname + '推荐就业码', // 分享标题  
								desc: '由' + that.list.nickname + '推荐就业码', // 分享描述  
								link: data.data.url, // 分享链接
								imgUrl: that.tg_img, // 分享图标                                
								success: res => {
									uni.showToast({
										title: '请点击右上角分享',
										icon: 'none'
									})
								},
								cancel: function() {
									console.log('设置分享内容失败')
									// 用户取消分享后执行的回调函数  
								}
							});
							jweixin.updateTimelineShareData({
								title: '由' + that.list.nickname + '推荐就业码', // 分享标题  
								desc: '由' + that.list.nickname + '推荐就业码', // 分享描述  
								link: data.data.url, // 分享链接
								imgUrl: that.tg_img, // 分享图标                                
								success: res => {
									uni.showToast({
										title: '请点击右上角分享',
										icon: 'none'
									})
								},
								cancel: function() {
									console.log('设置分享内容失败')
									// 用户取消分享后执行的回调函数  
								}
							});
						})
					} else {
						uni.showToast({
							title: data.msg,
							icon: 'none',
							duration: 2000
						})
					}
				}, params)
			},
			
			savephone() {
				uni.downloadFile({ //下载
					url: this.tk_img, //图片下载地址
					success: res => {
						if (res.statusCode === 200) {
							uni.saveImageToPhotosAlbum({ //保存图片到系统相册。
								filePath: res.tempFilePath, //图片文件路径
								success: function() {
									uni.showToast({
										title: '图片保存成功',
										icon: 'none',
									});
								},
								fail: function(e) {
									console.log(e);
									uni.showToast({
										title: '图片保存失败',
										icon: 'none',
									});
								}
							});
						}
					}
				});

			},
			getdata() {
				var that = this
				let params = {
					page: that.page
				}
				uni.showLoading({
					title: '加载中'
				})
				Goods2.tk((data) => {
					console.log(data)
					if (data.code === 0) {
						that.tk_img = data.data
					} else {
						uni.showToast({
							title: data.msg,
							icon: 'none',
							duration: 2000
						})
					}
					uni.hideLoading()
				}, params)
			}
		}
	}
</script>

<style>
	page {
		background-color: #fff;
	}
</style>